﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;

namespace JavascriptWebResourcesManager.Forms
{
    internal partial class UpdateForm : Form
    {
        #region Variables

        /// <summary>
        /// Current script
        /// </summary>
        Script currentScript;
        
        /// <summary>
        /// Xrm Organization Service
        /// </summary>
        IOrganizationService innerService;

        /// <summary>
        /// Current Prefix for names depending on selected solution
        /// </summary>
        string currentPrefix = string.Empty;

        #endregion 

        #region Constructor

        /// <summary>
        /// Initializes a new instance of class UpdateForm
        /// </summary>
        /// <param name="script">Script to display or to create</param>
        /// <param name="service">Xrm Organization Service</param>
        public UpdateForm(Script script, IOrganizationService service)
        {
            InitializeComponent();

            this.innerService = service;
            this.currentScript = script;

            this.FillControls();
        }

       
        #endregion Constructor

        #region Properties

        /// <summary>
        /// Script to update
        /// </summary>
        internal Script Script
        {
            get
            {
                return this.currentScript;
            }
        }

        #endregion Properties

        #region Methods

        private void FillControls()
        {
            if (!string.IsNullOrEmpty(this.currentScript.FilePath))
                this.txtPath.Text = this.currentScript.FilePath;

            if (this.currentScript.ScriptEntity.Contains("name"))
                this.txtName.Text = this.currentScript.ScriptEntity["name"].ToString();

            if (this.currentScript.ScriptEntity.Contains("displayname"))
                this.txtDisplayName.Text = this.currentScript.ScriptEntity["displayname"].ToString();

            if (this.currentScript.ScriptEntity.Contains("description"))
                this.txtDescription.Text = this.currentScript.ScriptEntity["description"].ToString();
        }
               
        #endregion

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.currentScript.ScriptEntity["displayname"] = this.txtDisplayName.Text;
            this.currentScript.ScriptEntity["description"] = this.txtDescription.Text;

            this.DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}
